<template>
      <!-- 总容器 -->
      <div class="wrapper">
        <header>
            <i class="fa fa-angle-left" onclick="history.go(-1)"></i>
            <p>我的预约</p>
            <div></div>
        </header>
        <div class="top-ban"></div>

        <ul>
            <li v-for="(order,index) in orderArr" :key="order.orderId">
                <div class="left">
                    <p>{{order.orderDate.substring(0,10)}}</p>
                    <p>{{order.setmeal.name}}</p>
                </div>
                <div class="right" style="cursor: pointer;" @click="cancelOrder(index)">
                    {{order.isCancle==1?"取消订单":""}}
                </div>
            </li>
        </ul>
        
        <div class="bottom-ban">
        <Foot></Foot>
        </div>
    </div>

</template>

<script>
import Foot from '../components/Foot.vue'
import { toRefs,toRef,reactive } from 'vue';
import axios from '../axios';
import { useRouter } from 'vue-router';
import { getSessionStorage } from '../common';

export default {
    components:{Foot},

    setup(){

        const state = reactive({
            orderArr:[],
            userId:getSessionStorage('users').userId
        })

        loadAppointmentByUser()
        function loadAppointmentByUser(){
            axios.post('/api/queryAppointmentByUser',{
                    userId: state.userId,
                    state: 2
                    },{
                    headers: {
                        'token': getSessionStorage("token")
                    }
                    })
                    .then(function (response) {
                        console.log(response);
                        state.orderArr = response.data.data

                    })
                    .catch(function (error) {
                    console.log(error);
                    })
        }

        function cancelOrder(index){
            //1.先取消订单
            axios.post('/api/cancelOrderByOrderId',{
                    orderId:state.orderArr[index].orderId
                    },{
                    headers: {
                        'token': getSessionStorage("token")
                    }
                    })
                    .then(function (response) {
                        console.log(response);
                        loadAppointmentByUser()
                    })
                    .catch(function (error) {
                    console.log(error);
                    })


            //2.跳转到
        }

        return{
            ...toRefs(state),
            loadAppointmentByUser,
            cancelOrder
        }
    }


}
</script>


<style>
/*********************** 总容器 ***********************/
.wrapper{
    width: 100%;
    height: 100%;
    background-color: #F9F9F9;
}

/*********************** header ***********************/
header{
    width: 100%;
    height: 15.7vw;
    background-color: #FFF;

    position: fixed;
    left: 0;
    top: 0;

    display: flex;
    align-items: center;
    justify-content: space-between;

    box-sizing: border-box;
    padding: 0 3.6vw;
}
header .fa{
    font-size: 8vw;
}
/*********************** common样式 ***********************/
.top-ban{
    width: 100%;
    height: 15.7vw;
}
.bottom-ban{
    width: 100%;
    height: 14.2vw;
}

/*********************** ul ***********************/
ul{
    width: 86vw;
    margin: 0 auto;
}
ul li{
    width: 100%;
    height: 14vw;
    border-bottom: solid 1px #EEE;

    display: flex;
    justify-content: space-between;
    align-items: center;
}
ul li .left{
    user-select: none;
    cursor: pointer;
}
ul li .left p:first-child{
    color: #555;
    font-size: 3.3vw;
}
ul li .left p:last-child{
    color: #333;
    font-size: 4.2vw;
    font-weight: 600;
}
ul li .right{
    font-size: 4vw;
    color: #E6A23C;
}
</style>